<?php
class InforsController extends AppController {

	var $name = 'Infors';
	
	var $uses= array('Member');

	var $components = array("Tree");
	
	/**
	 * 
	 * Enter description here ...
	 */
	function index() {
		$a=array(1,2,3,5,7);
		$b=array(1,2,3);
		$c=array_diff($a,$b);
		//pr($c);die();
		
		// tinh thanh tich nhom
		
		$ghk=$this->Member->findById(1);		
		$directChildren = $this->Member->children(1);
	  //pr($directChildren);
		$tol=$ghk['Member']['gt'];
		foreach($directChildren as $chid){
			if($chid['Member']['levels'] >= 5){
				$arr[]=$chid;
				$kk[]=$chid['Member']['id'];
			}
			
			$tol=$tol+$chid['Member']['gt'];
		}
		//pr($kk);
   // tinh tong nhung thang co level cao hon level dang xet  hoac ngang cap		
   		$bitru=0;
		foreach($arr as $abc){
					foreach($arr as $bce){
						if($bce['Member']['parent_id']== $abc['Member']['id']){
							$brr[]=$bce;
							$cc[]=$bce['Member']['id'];
						}
					
					}
			
		}
		//echo '----------------';
			$c=array_diff($kk,$cc);
			//echo '----------------';
			$tong_tru=0;
			$i=0;
			foreach($c as $ID){
				$id=$this->Member->findById($ID);				
				$tong_tru=$tong_tru + $id['Member']['gt'];			
				$ch=$this->Member->children($ID);				
				foreach($ch as $k){
					$tong_tru=$tong_tru+$k['Member']['gt'];
				}
				
			}
			//echo '---------------------';
			//pr($c);
		     //echo 'tong thu nhap toan mang luoi'.pr($tol);
			// echo '---------------------';
			 // echo 'Tong thu nhap cac cap level bang hoac lon hon cacp dang set'.pr($tong_tru);
			
			// $tt_nhom=$tol -$tong_tru ;
			
			// echo 'Ket thanh tich nhom'.($tt_nhom);
			//die();
		
	}
	// lay tat ca lelvel lon  hon level dang set => loc tat cac cac con co ai 
	
	
	//kiem tra co con khong
	//lay cac con 
	function carts() {

	}
	function business() {

	}

	function maps() {
		$id_user=$this->Session->read("tkid");
		$members=$this->Member->find('all',array('conditions'=>array('Member.parent_id'=>$id_user)));
		$this->set('members',$members);

	}
	function Profile() {
		$id_user=$this->Session->read("tkid");	
		$this->data=$this->Member->read(null,$this->Session->read("tkid"));			
	}
	function edit() {
		if (!empty($this->data)) {
			if ($this->Member->save($this->data)) {
				$this->Session->setFlash(__('Sửa thành công', true));
				$this->redirect(array('action' => 'profile'));
			} else {
				$this->Session->setFlash(__('Không sửa được vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data=$this->Member->read(null,$this->Session->read("tkid"));
		}			
	}
	function changepass() {

	}
	
	function testTree(){
		$this->Tree->import();
		$this->Tree->calculateGroupPv();
		$this->Tree->calculateDirectCommission();
		$this->Tree->calculateIndirectCommission();
		$this->Tree->printTree();die;
	}
}
?>